{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 列表简介\n",
    "\n",
    "- 列表是 Python 中的一个对象\n",
    "- 对象（object）是内存中专门用来存储数据的一块区域\n",
    "- 之前学习的对象（如数值）只能保存一个单一的数据\n",
    "- 列表中可以保存多个有序的数据\n",
    "- 列表是存储对象的对象\n",
    "- 列表的使用：\n",
    "    1. 列表的创建\n",
    "    2. 操作列表中的数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 列表的创建\n",
    "\n",
    "- 通过 `[]` 创建列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_list = []\n",
    "print(f'my_list = {my_list},type = {type(my_list)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 列表存储的数据称为元素\n",
    "- 一个列表中可以存储多个元素，也可以在创建列表时，来指定列表中的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_list = [10]\n",
    "print(f'my_list = {my_list},type = {type(my_list)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 当向列表中添加多个元素时，多个元素之间使用 `,` 隔开"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_list = [10,20,30,40,50]\n",
    "print(f'my_list = {my_list},type = {type(my_list)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 列表中可以保存任意的对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_list = [10,'Hello World',True,None,[1,2,3],print]\n",
    "print(f'my_list = {my_list},type = {type(my_list)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 列表中的对象都会按照插入的顺序储存到列表中，第一个插入的对象保存到第一个位置，第二个保存到第二个位置...\n",
    "- 可以通过索引（index）来获取列表中的元素\n",
    "    - 索引是元素在列表中的位置\n",
    "    - 列表中的每一个元素都有一个索引\n",
    "    - 索引是从 `0` 开始的整数\n",
    "    - 语法：`列表名[索引]`\n",
    "    - 如果使用的索引超过了最大的范围，会抛出异常 `IndexError: list index out of range`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_list = [1,2,3,4,5]\n",
    "print(f'my_list[0] = {my_list[0]},my_list[4] = {my_list[4]}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_list = [1,2,3,4,5]\n",
    "print(f'my_list[5] = {my_list[5]}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 通过 `len()` 函数可以获取列表的长度\n",
    "    - 获取到的长度是列表 `最大索引 + 1`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_list = [1,2,3,4,5]\n",
    "print(f'len = {len(my_list)},my_list[{len(my_list) - 1}] = {my_list[len(my_list) -1]}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_city = ['Guang Zhou','Wu Han','New York','Tokyo','Birmingham']\n",
    "\n",
    "i = 0\n",
    "while i < len(my_list):\n",
    "    print(f'{i + 1}. {my_city[i]}')\n",
    "    i += 1"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "e7370f93d1d0cde622a1f8e1c04877d8463912d04d973331ad4851f04de6915a"
  },
  "kernelspec": {
   "display_name": "Python 3.9.4 64-bit",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.4"
  },
  "orig_nbformat": 2
 },
 "nbformat": 4,
 "nbformat_minor": 2
}